package test.kryonet.server;

import java.io.IOException;
import java.util.Scanner;

import test.kryonet.network.DataType1;
import test.kryonet.network.Network;



import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;

public class RunClass {
	static String k = " ssdd";
	
	public static void main(String[] args ) {
		final Server server = new Server();
		Network.register(server);
		server.addListener(new Listener(){
			@Override
			public void received(Connection arg0, Object arg1) {
				//System.out.println("Server> recived something");
				if ( arg1 instanceof String ) {
					System.out.println(arg0.getRemoteAddressTCP() + " says :" + arg1 );
				}
				
				if ( arg1 instanceof DataType1 ) {
					if ( ((DataType1)arg1) == DataType1.ANENUM ) System.out.println("ANEMUM");
				}
			}

			@Override
			public void connected(Connection arg0) {
				System.out.println("Server> connected from #" + arg0.getID()+"#"+ arg0.getRemoteAddressTCP());
			}
			
			@Override
			public void disconnected(Connection arg0) {
				System.out.println("Server> disconnected from #" + arg0.getID());
			}
		});
		try {
			server.bind(Network.PORT_TCP);
		} catch (NumberFormatException | IOException e) {
			System.out.println("Error: server cant bind");
			System.out.println(-1);
		}
		
		new Thread(server).start();
		
		new Thread() {
			public void run() {
				Scanner in = new Scanner(System.in);
				char i = 'a';
				do {
					if (in.hasNext())
						i = in.next().charAt(0);
				} while (i != 'q');
				server.stop();
				in.close();
			};
		}.start();
	}
}
